<?php
$conn = mysqli_connect("localhost", "root", "", "taxi");
if (!$conn) {
    die("Ошибка: " . mysqli_connect_error());
}
?>
<!DOCTYPE html>
<html>
	<head>
		<!-- Mobile Specific Meta -->
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<!-- Favicon-->
		<link rel="shortcut icon" href="img/fav.png">
		<!-- Author Meta -->
		<meta name="author" content="colorlib">
		<!-- Meta Description -->
		<meta name="description" content="">
		<!-- Meta Keyword -->
		<meta name="keywords" content="">
		<!-- meta character set -->
		<meta charset="UTF-8">
		<!-- Site Title -->
		<title>Taxi</title>

		<link href="https://fonts.googleapis.com/css?family=Poppins:100,200,400,300,500,600,700" rel="stylesheet"> 
			<!--
			CSS
			============================================= -->
			<link rel="stylesheet" href="css/linearicons.css">
			<link rel="stylesheet" href="css/font-awesome.min.css">
			<link rel="stylesheet" href="css/bootstrap.css">
			<link rel="stylesheet" href="css/magnific-popup.css">
			<link rel="stylesheet" href="css/nice-select.css">							
			<link rel="stylesheet" href="css/animate.min.css">
			<link rel="stylesheet" href="css/jquery-ui.css">			
			<link rel="stylesheet" href="css/main.css">
			
		</head>
<body>
<?php
// если запрос GET
if($_SERVER["REQUEST_METHOD"] === "GET" && isset($_GET["id"]))
{
    $userid = mysqli_real_escape_string($conn, $_GET["id"]);
    $sql = "SELECT * FROM auto WHERE id_car = '$userid'";
    if($result = mysqli_query($conn, $sql)){
        if(mysqli_num_rows($result) > 0){
            foreach($result as $row){
                $photo_car = $row["photo_car"];
                $name_car = $row["name_car"];
            }
            echo "<div class='container'>
			<h3>Редактирование авто $name_car</h3>
                <form method='post' enctype = 'multipart/form-data'>
					<div class='row'>
                    <input type='hidden' name='id_car' value='$userid'/>
                    <p>Фото:
                    <br><input type='text' name='photo_car' value='$photo_car'/></p>
                    <p>Название:
                    <br><input type='text' name='name_car' value='$name_car'/></p>
                    <br><input type='submit' value='Сохранить' class='btn'>
				</form>
				<br><a class='btn' href='add_car.php' style='padding-top: 15px;'>Назад</a>
				</div>
				</div>";
        }
        else{
            echo "<div>Пользователь не найден</div>";
        }
        mysqli_free_result($result);
    } else{
        echo "Ошибка: " . mysqli_error($conn);
    }
}
elseif (isset($_POST["id_car"]) && isset($_POST["photo_car"]) && isset($_POST["name_car"])) {
      
    $userid = mysqli_real_escape_string($conn, $_POST["id_car"]);
    $photo_car = mysqli_real_escape_string($conn, $_POST["photo_car"]);
    $name_car = mysqli_real_escape_string($conn, $_POST["name_car"]);
      
    $sql = "UPDATE auto SET photo_car = '$photo_car', name_car = '$name_car' WHERE id_car = '$userid'";
    if($result = mysqli_query($conn, $sql)){
        header("Location: add_car.php");
    } else{
        echo "Ошибка: " . mysqli_error($conn);
    }
}
else{
    echo "Некорректные данные";
}
mysqli_close($conn);
?>
</body>
</html>